var pics=new Array();
var thumbs=new Array();
var imgs=new Array();
var status=new Array();
var remarks=new Array();
/**
 *  图片显示特效代码 
*/
function enableFlyout(){
	$('div#img_box .thumbs_div a.gallery_a').flyout({
		inOpacity:0,
		fullSizeImage:true,
		inSpeed:1000
	});
}

/**
 * 重建图片显示区
 */
function rebuildBox(){
	var template="<div class='thumbs_div'>" +
			"<a class='gallery_a' href='$img'><img class='thumbs' src='$thumb'/></a><br/>" +
			"<a href='javascript:void(0)' onclick='editRemark($index)'>编辑说明</a>&nbsp;&nbsp;" +
			"<a href='javascript:void(0)' onclick='deletePicture($index)'>删除</a></div>";
	var box=document.getElementById("img_box");
	var html="";
	for(var i=0;i<imgs.length;i++){
		if(status[i]=="deleted")
			continue;
		html+=template.replace("$img", imgs[i]).replace(/\$index/g,i).replace("$thumb", thumbs[i]);
	}
	box.innerHTML=html;
	enableFlyout();
}

/**
 * 上传结束回传照片
 * @param thumb_url
 * @param img_url
 * @param error
 */
function upload_done(pid,thumb_url,img_url,error){
	if(error.length>0){
		alert(error);
		return;
	}
	var index=imgs.length;
	thumbs[index]=thumb_url;
	imgs[index]=img_url;
	status[index]="";
	pics[index]=pid;
	remarks[index]="";
	rebuildBox();
	closeDiv('upload');
}

/**
 * 打开上传窗口
 */
function open_upload_form(){
	document.getElementById("upload_form").src="admin/face/upload/upload_image.jsp";
	showDiv('upload','event',300,300);
}

/**
 * 删除图片
 * @param index
 */
function deletePicture(index){
	status[index]="deleted";
	rebuildBox();
}

var currentRemarkIndex=0;

function editRemark(index){
	currentRemarkIndex=index;
	var area=document.getElementById("remark_area");
	area.value=remarks[index];
	showDiv('remark','event',300,300);
}

function editRemarkDone(){
	var area=document.getElementById("remark_area");
	remarks[currentRemarkIndex]=area.value.replace(/\;/g,"\\;");
	area.value="";
	closeDiv('remark');
}

function doSubmit(form){
	var pic_content=pics.length+">";
	for(var i=0;i<pics.length;i++){
		if(status[i]=="deleted")
			pic_content+="!";
		pic_content+=pics[i]+":"+remarks[i]+";";
	}
	form.picture.value=pic_content;
	return true;
}

window.onload=function(){
	var i=0;
	var pic_id=null;
	while(true){
		pic_id=document.getElementById("initial_data_"+i+"_id");
		if(pic_id==null)
			break;
		pics[i]=pic_id.innerHTML;
		thumbs[i]=document.getElementById("initial_data_"+i+"_thumb").innerHTML;
		imgs[i]=document.getElementById("initial_data_"+i+"_path").innerHTML;
		remarks[i]=document.getElementById("initial_data_"+i+"_remark").innerHTML;
		i++;
	}
	rebuildBox();
};